/*
 * @Description: 
 * @Version: 2.0
 * @Autor: ftt
 * @Date: 2022-06-11 09:23:08
 * @LastEditors: TT
 * @LastEditTime: 2022-07-11 15:44:10
 */

import 'package:hzy_normal_widget/hzy_normal_widget.dart';
import 'package:hzy_template/config/controller/launch_change_c.dart';
import '../../components/common_index.dart';
import '../../pages/login_module/routers/login_page_id.dart';
import '../../utils/common_sp_util.dart';
import '../config_index.dart';
import '../models/user_model.dart';

class UserStateController extends GetxController {
  static UserStateController get current => Get.find<UserStateController>();

  final Rx<UserModel> _currentUser = UserModel().obs;
  UserModel get user => _currentUser.value;

  static bool get isLogin => current.user.nickname != null;

  /// 登录完成
  loadSuccess(UserModel model) async {
    CommonSpUtil.saveUserToken(token: model.nickname ?? "");
    CommonSpUtil.saveUserInfo(info: model.toMap());
    _currentUser.value = model;
  }

  /// 退出登录
  void exitloginSuccess() {
    CommonSpUtil.saveUserToken(token: "");
    _currentUser.value = UserModel();
    showtoastmsg("欢迎再次回来", ontap: () {
      currentToPage(LoginPageId.login);
    });
  }
}

class AllControllerBinding implements Bindings {
  @override
  void dependencies() {
    Get.put<UserStateController>(
      UserStateController(),
      permanent: true,
    );
    Get.put<LaunchChangeC>(
      LaunchChangeC(),
      permanent: true,
    );
    GloblConfig.initnormaldata();
  }
}
